﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OneMake.DataAccess;
using System.Windows.Forms.DataVisualization.Charting;
using OneMake.Model;

namespace OneMake.Winforms
{
    public partial class uc_report_customer_unit : UserControl, IBaseReportCtrl
    {
        private DataHelper dataHelper = new DataHelper();
        private ReportHelper rh = new ReportHelper();

        public uc_report_customer_unit()
        {
            InitializeComponent();
        }

        private void uc_report_customer_unit_Load(object sender, EventArgs e)
        {
            dtpBegin.Value = DateTime.Now.AddDays(-7);
            dtpEnd.Value = DateTime.Now;
            LoadBrands();
            if (cbBrands.Items.Count > 0)
                cbBrands.SelectedIndex = 0;
        }
        
        private void LoadBrands()
        {
            List<string> brands = rh.get_report_customer_unit_brands();
            foreach(string brand in brands)
            {
                if (!string.IsNullOrEmpty(brand))
                    cbBrands.Items.Add(brand);
            }
        }

        public string ReportName
        {
            get { return "report_customer_units"; }
        }

        public DockStyle Dock2 
        {
            get { return this.Dock; }
            set { this.Dock = value; }
        }

        public Control Parent2
        {
            get { return this.Parent; }
            set { this.Parent = value; }
        }

        public void LoadData()
        {
            DateTime selBeginTime = dtpBegin.Value;
            DateTime selEndTime = dtpEnd.Value;

			lbProgress.Visible = true;
			panel1.Refresh();

            string selBrand = cbBrands.Text;

            Dictionary<string, string> attrMap = dataHelper.GetTableFieldMap(ReportName);
            dgvReport.Columns.Clear();
            foreach (KeyValuePair<string, string> item in attrMap)
            {
                DataGridViewColumn col = new DataGridViewTextBoxColumn();
                col.Name = item.Key;
                col.HeaderText = item.Value;
                col.DataPropertyName = item.Key;
                if (col.Name == "per_person")
                    col.DefaultCellStyle.Format = "0.00%";
                dgvReport.Columns.Add(col);
            }

            List<report_customer_units> report = rh.get_report_customer_unit(selBrand, selBeginTime.Date, selEndTime.Date);

            dgvReport.DataSource = report;

            lbProgress.Visible = false;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            LoadData();
        }

    }
}
